package org.mybatis.generator.codegen.mybatis3.javamapper.elements;

import org.mybatis.generator.api.dom.java.*;

import java.util.Set;
import java.util.TreeSet;

public class UpdateBySelectiveWithIdsMethodGenerator extends
        AbstractJavaMapperMethodGenerator {
    public UpdateBySelectiveWithIdsMethodGenerator() {
        super();
    }

    @Override
    public void addInterfaceElements(Interface interfaze) {
        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
        FullyQualifiedJavaType parameterType;

        if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
            parameterType = new FullyQualifiedJavaType(introspectedTable
                    .getRecordWithBLOBsType());
        } else {
            parameterType = new FullyQualifiedJavaType(introspectedTable
                    .getBaseRecordType());
        }
        FullyQualifiedJavaType listType = new FullyQualifiedJavaType("java.util.List");
        FullyQualifiedJavaType paramType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param");

        importedTypes.add(parameterType);
        importedTypes.add(listType);
        importedTypes.add(paramType);

        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.setName(introspectedTable
                .getUpdateBySelectiveWithIds());
        Parameter record = new Parameter(parameterType, "record");
        record.addAnnotation("@Param(\"record\")");
        method.addParameter(record); //$NON-NLS-1$

        listType.addTypeArgument( new FullyQualifiedJavaType("java.lang.Integer"));
        Parameter parameter = new Parameter(listType, "ids");
        parameter.addAnnotation("@Param(\"list\")");
        method.addParameter(parameter); //$NON-NLS-1$

        context.getCommentGenerator().addGeneralMethodComment(method,
                introspectedTable);

        addMapperAnnotations(method);

        if (context.getPlugins()
                .clientUpdateByPrimaryKeySelectiveMethodGenerated(method,
                        interfaze, introspectedTable)) {
            addExtraImports(interfaze);
            interfaze.addImportedTypes(importedTypes);
            interfaze.addMethod(method);
        }
    }

    public void addMapperAnnotations(Method method) {
    }

    public void addExtraImports(Interface interfaze) {
    }
}
